﻿using DatabaseExtension;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using DatabaseExtension.ConnectionString;
using System.Linq;
using Oracle.DataAccess.Client;
using SQLiteProvider.ConnectionString;

namespace Test
{
    
    
    /// <summary>
    ///Dies ist eine Testklasse für "DbInterfaceTest" und soll
    ///alle DbInterfaceTest Komponententests enthalten.
    ///</summary>
    [TestClass()]
    public class DbInterfaceTestParam
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Ruft den Testkontext auf, der Informationen
        ///über und Funktionalität für den aktuellen Testlauf bietet, oder legt diesen fest.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Zusätzliche Testattribute
        // 
        //Sie können beim Verfassen Ihrer Tests die folgenden zusätzlichen Attribute verwenden:
        //
        //Mit ClassInitialize führen Sie Code aus, bevor Sie den ersten Test in der Klasse ausführen.
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Mit ClassCleanup führen Sie Code aus, nachdem alle Tests in einer Klasse ausgeführt wurden.
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Mit TestInitialize können Sie vor jedem einzelnen Test Code ausführen.
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Mit TestCleanup können Sie nach jedem einzelnen Test Code ausführen.
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion
        #region SqlCe
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSqlCeParamString2Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String  expected = "te",
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", "test", 2);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSqlCeParamString3Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String  expected = "te",
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", "test", System.Data.DbType.String, 2);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSqlCeParamString4_1Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String  expected = "te",
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", "test", System.Data.SqlDbType.NVarChar, true, 2);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSqlCeParamString4_2Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String expected = null,
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", null, System.Data.SqlDbType.NVarChar, true, 2);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (DBNull.Equals(test, DBNull.Value) == true ? null : test.ToString());
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void DbInterfaceSqlCeParamString4_3Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String expected = null,
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", null, MySql.Data.MySqlClient.MySqlDbType.String, true, 2);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (DBNull.Equals(test, DBNull.Value) == true ? null : test.ToString());
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSqlCeParamString5_1Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String expected = "te",
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", "test", System.Data.DbType.String, true, 2);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test Sql Compact edition connection. Create and delete db with parameter.
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSqlCeParamString5_2Test()
        {
            String server = System.IO.Path.Combine(Environment.CurrentDirectory, "tmp.sdf");
            String expected = null,
                    actual = String.Empty;
            using (DbInterface target = new DbInterface(new SqlCeConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", null, System.Data.DbType.String, true, 2);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (DBNull.Equals(test, DBNull.Value) == true ? null : test.ToString());
            }
            System.IO.File.Delete(server);
            Assert.AreEqual(expected, actual);
        }
        #endregion
        #region SQLite
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSQLiteMemParamString2Test()
        {
            String server = ":memory:";
            String expected = "test";
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSQLiteMemParamString3Test()
        {
            String server = ":memory:";
            String expected = "test";
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected, System.Data.DbType.String, 0);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSQLiteMemParamString4_1Test()
        {
            String server = ":memory:";
            String expected = "test";
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected, (Object)System.Data.DbType.String, true, 5);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSQLiteMemParamString4_2Test()
        {
            String server = ":memory:";
            String expected = null;
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected, (Object)System.Data.DbType.String, true, 5);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (DBNull.Equals(test, DBNull.Value) == true ? null : test.ToString());
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void DbInterfaceSQLiteMemParamString4_3Test()
        {
            String server = ":memory:";
            String expected = null;
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected, (Object)System.Data.SqlDbType.NVarChar, true, 5);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (DBNull.Equals(test, DBNull.Value) == true ? null : test.ToString());
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSQLiteMemParamString5_1Test()
        {
            String server = ":memory:";
            String expected = "test";
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected, System.Data.DbType.String, true, 5);
                actual = target.SqlExecuteScalar("SELECT @test").ToString();
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test SQlite connection. Create in memory
        ///</summary>
        [TestMethod()]
        public void DbInterfaceSQLiteMemParamString5_2Test()
        {
            String server = ":memory:";
            String expected = null;
            String actual = String.Empty;
            using (DbInterface target = new DbInterface(new SQLiteConnectionInfo(server, "test") { FailIfMissing = false }))
            {
                target.SetSqlParameter("@test", expected, System.Data.DbType.String, true, 5);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (DBNull.Equals(test, DBNull.Value) == true ? null : test.ToString());
            }
            Assert.AreEqual(expected, actual);
        }
        #endregion
        #region MS SQL
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceMSSQLParam2Test()
        {
            long expected = 1;
            long actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", 1);
                actual = (int)target.SqlExecuteScalar("SELECT @test");
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceMSSQLParam3Test()
        {
            long expected = 1;
            long actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", 1, System.Data.DbType.Int32, 0);
                actual = (int)target.SqlExecuteScalar("SELECT @test");
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceMSSQLParam4_1Test()
        {
            int? expected = null;
            int? actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", null, System.Data.SqlDbType.Int, true, 0);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (int?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceMSSQLParam4_2Test()
        {
            int? expected = 1;
            int? actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", 1, System.Data.SqlDbType.Int, true, 0);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (int?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceMSSQLParam5_1Test()
        {
            int? expected = null;
            int? actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", null, System.Data.SqlDbType.Int, true, 0);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (int?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceMSSQLParam5_2Test()
        {
            int? expected = 1;
            int? actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", 1, System.Data.SqlDbType.Int, true, 0);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (int?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test MSSQL connection. SELECT 1
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void DbInterfaceMSSQLParam5_3Test()
        {
            int? expected = 1;
            int? actual = -1;
            using (DbInterface target = new DbInterface(new SqlConnectionInfo("localhost") { IntegratedSecurity = true }))
            {
                target.SetSqlParameter("@test", 1, MySql.Data.MySqlClient.MySqlDbType.Int16, true, 0);
                Object test = target.SqlExecuteScalar("SELECT @test");
                actual = (int?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        #endregion
        #region Ole DB
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceOleDbToMSSQL2008Param2Test()
        {
            long expected = 1;
            long actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", 1);
                actual = (int)target.SqlExecuteScalar("SELECT ?");
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceOleDbToMSSQL2008Param3Test()
        {
            Int16 expected = 1;
            Int16 actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", 1, System.Data.DbType.Int16);
                actual = (Int16)target.SqlExecuteScalar("SELECT ?");
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceOleDbToMSSQL2008Param4_1Test()
        {
            Int16 expected = 1;
            Int16 actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", 1, System.Data.DbType.Int16, false);
                actual = (Int16)target.SqlExecuteScalar("SELECT ?");
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceOleDbToMSSQL2008Param4_2Test()
        {
            Int16? expected = null;
            Int16? actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", null, System.Data.DbType.Int16, true);
                Object test = target.SqlExecuteScalar("SELECT ?");
                actual = (Int16?)(DBNull.Equals(test, DBNull.Value) == true ? null: test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceOleDbToMSSQL2008Param5_1Test()
        {
            Int16? expected = 1;
            Int16? actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", 1, (object)System.Data.DbType.Int16, true);
                Object test = target.SqlExecuteScalar("SELECT ?");
                actual = (Int16?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        public void DbInterfaceOleDbToMSSQL2008Param5_2Test()
        {
            Int16? expected = null;
            Int16? actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", null, (object)System.Data.DbType.Int16, true);
                Object test = target.SqlExecuteScalar("SELECT ?");
                actual = (Int16?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Test OleDb on Microsoft connection. SELECT 1
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void DbInterfaceOleDbToMSSQL2008Param5_3Test()
        {
            Int16? expected = null;
            Int16? actual = -1;
            var con = new OleDbConnectionInfo("localhost", null, null) { Provider = "SQLNCLI10" };
            con.OleDbConnectionBuilder.Add("Trusted_Connection", "yes");
            con.OleDbConnectionBuilder.Add("Server", "localhost");
            using (DbInterface target = new DbInterface(con))
            {
                target.SetSqlParameter("?", null, MySql.Data.MySqlClient.MySqlDbType.Int16, true);
                Object test = target.SqlExecuteScalar("SELECT ?");
                actual = (Int16?)(DBNull.Equals(test, DBNull.Value) == true ? null : test);
            }
            Assert.AreEqual(expected, actual);
        }
        #endregion
    }
}
